package netty.demo.nio.fileChannel;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Zed
 * @description
 * @date 2022/10/22 21:50
 */
public class testFilesWalkFileTree {
    public static void main(String[] args) throws Exception {
        AtomicInteger dirCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("D:\\JDK"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().contains(".jar")) {
                    System.out.println(file);
                    dirCount.incrementAndGet();
                }
                return super.preVisitDirectory(file, attrs);
            }
        });
        System.out.println("jar count:" + dirCount);
    }


    public static void m1(String[] args) throws Exception {
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("D:\\Java书籍"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("----------->" + dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });
        System.out.println("dir count:" + dirCount);
        System.out.println("file count:" + fileCount);
    }
}
